package com.jwaoo.chat.session.recent.holder;
import com.jwaoo.chat.ChatConstants;
import com.jwaoo.chat.fragment.session.bean.SessionBean;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseQuickAdapter;

public class SessionViewHolder extends RecentViewHolder {

    public SessionViewHolder(BaseQuickAdapter adapter) {
        super(adapter);
    }

    @Override
    protected String getContent(SessionBean recent) {
        return descOfMsg(recent);
    }

    @Override
    protected String getOnlineStateContent(SessionBean recent) {
//        if (recent.getSessionType() == SessionTypeEnum.P2P && NimUIKit.enableOnlineState()) {
//            return NimUIKit.getOnlineStateContentProvider().getSimpleDisplay(recent.getContactId());
//        } else {
//            return super.getOnlineStateContent(recent);
//        }
        return null;
    }

    protected String descOfMsg(SessionBean recent) {
        if (recent.getMsgtype() == ChatConstants.text) {
            return recent.getMsg();
        } else {
            String digest = null;
            if (getCallback() != null) {
                digest = getCallback().getDigestOfAttachment(recent.getMsgtype());
            }
            if (digest == null) {
                digest = getDefaultDigest(recent);
            }

            return digest;
        }
    }

    // SDK本身只记录原始数据，第三方APP可根据自己实际需求，在最近联系人列表上显示缩略消息
    // 以下为一些常见消息类型的示例。
    private String getDefaultDigest(SessionBean recent) {
        switch (recent.getMsgtype()) {
            case ChatConstants.text:
                return recent.getMsg();
            case ChatConstants.image:
                return "[图片]";
            case ChatConstants.video:
                return "[视频]";
            case ChatConstants.audio:
                return "[语音消息]";
            case ChatConstants.location:
                return "[位置]";
            case ChatConstants.file:
                return "[文件]";
            case ChatConstants.tip:
//                List<String> uuids = new ArrayList<>();
//                uuids.add(recent.getRecentMessageId());
//                List<IMMessage> messages = NIMClient.getService(MsgService.class).queryMessageListByUuidBlock(uuids);
//                if (messages != null && messages.size() > 0) {
//                    return messages.get(0).getContent();
//                }
                return "[通知提醒]";
            case ChatConstants.notification:
//                return TeamNotificationHelper.getTeamNotificationText(recent.getContactId(),
//                        recent.getFromAccount(),
//                        (NotificationAttachment) recent.getAttachment());
            case ChatConstants.avchat:
//                AVChatAttachment avchat = (AVChatAttachment) attachment;
//                if (avchat.getState() == AVChatRecordState.Missed && !recent.getFromAccount().equals(NimUIKit.getAccount())) {
//                    // 未接通话请求
//                    StringBuilder sb = new StringBuilder("[未接");
//                    if (avchat.getType() == AVChatType.VIDEO) {
//                        sb.append("视频电话]");
//                    } else {
//                        sb.append("音频电话]");
//                    }
//                    return sb.toString();
//                } else if (avchat.getState() == AVChatRecordState.Success) {
//                    StringBuilder sb = new StringBuilder();
//                    if (avchat.getType() == AVChatType.VIDEO) {
//                        sb.append("[视频电话]: ");
//                    } else {
//                        sb.append("[音频电话]: ");
//                    }
//                    sb.append(TimeUtil.secToTime(avchat.getDuration()));
//                    return sb.toString();
//                } else {
//                    if (avchat.getType() == AVChatType.VIDEO) {
//                        return ("[视频电话]");
//                    } else {
//                        return ("[音频电话]");
//                    }
//                }
            default:
                return "[自定义消息] ";
        }
    }
}
